// app.js
import { $http } from '@escook/request-miniprogram'
wx.http = $http
wx.http.baseUrl = 'https://live-api.itheima.net'
wx.http.beforeRequest = function (options) {
  // do somethimg...
  // console.log(options, 'options')
  // 判断用户是否登录,从getApp()中获取token
  const token = getApp().globalData.token
  if (token) {
    options.header = {
      'Authorization': 'Bearer '+ token
    }
  }
}

App({
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
  },
  globalData: {
    userInfo: null,
    // token从本地存储中获取
    token: wx.getStorageSync('token') || ''
  }
})
